﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace BankSystemWithCSharp.DataBase
{
    public partial class BankDataBase : IDataBase
    {
        // 单例模式
        public static BankDataBase Intance { get; } = new BankDataBase();

        private Dictionary<string, BankAccount> accounts = new Dictionary<string, BankAccount>();
        private List<TransformRecord> transforms = new List<TransformRecord>();
        private BankAccount loginAccount = null;

        public void Login(string accountNumber, string password)
        {
            ConfirmAccountFormat(accountNumber);
            ConfirmAccountExist(accountNumber);
            ConfirmPasswordFormat(password);
            accounts[accountNumber].ConfirmPassword(password);
            
            loginAccount = accounts[accountNumber];
        }

        public void Logout()
        {
            loginAccount = null;
        }

        public void Register(string accountNumber, string password)
        {
            ConfirmAccountFormat(accountNumber);
            ConfirmAccountNotExist(accountNumber);
            ConfirmPasswordFormat(password);

            BankAccount account = new BankAccount(accountNumber, password);
            accounts[accountNumber] = account;
        }

        public double GetMoneyNumber()
        {
            ConfirmLogin();

            return loginAccount.Money;
        }

        public void SaveMoney(double money)
        {
            ConfirmLogin();

            transforms.Add(new TransformRecord(
                GenerateID(),
                DateTime.Now,
                "现金",
                loginAccount.ToString(),
                money));
            loginAccount.Money += money;
        }

        public void WithdrawMoney(double money)
        {
            ConfirmLogin();
            ConfirmMoneyEnough(money);

            transforms.Add(new TransformRecord(
                GenerateID(),
                DateTime.Now,
                loginAccount.ToString(),
                "现金",
                money));
            loginAccount.Money -= money;
        }

        public void TransferMoney(string accountNumber, double money)
        {
            ConfirmLogin();
            ConfirmMoneyEnough(money);
            ConfirmAccountFormat(accountNumber);
            ConfirmAccountExist(accountNumber);

            transforms.Add(new TransformRecord(
                GenerateID(),
                DateTime.Now,
                loginAccount.ToString(),
                accountNumber.ToString(),
                money));
            loginAccount.Money -= money;
            accounts[accountNumber].Money += money;
        }
    }
}
